/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.String;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import modelo.ConsultasExterna;
import persistencia.BaseDatosConsultasExternas;

//import HistorialClinico;
//import persistencia.BaseDatos;

/**
 *
 * @author jony
 */
public class JPCrearConsultasExternas extends javax.swing.JPanel {

    /**
     * Creates new form JPCrearConsultasExternas
     */
    public JPCrearConsultasExternas() {
        initComponents();
        
        
      //TOOLTIP
       jtfEspecialidad.setToolTipText("Escribe aquí la especialidad. Por ejemplo. ");
       jtfFecha.setToolTipText("Escribe la fecha de hoy. Por ejemplo. 20/02/2015");
       jtfHoraEntrada.setToolTipText("Escribe la hora de entrada. Por ejemplo. 13:30");
       jtfHoraSalida.setToolTipText("Escribe la hora de salida. Por ejemplo. 15:30");
       jtfIdProcesoAsistencial.setToolTipText("Escribe aquí el id del paciente. Por ejemplo. 2652");

          //Pongo todos los TextField para que se pueda escribir
        jtfEspecialidad.setEditable(true);
        jtfFecha.setEditable(true);
        jtfHoraEntrada.setEditable(true);
        jtfHoraSalida.setEditable(true);
        jtfIdProcesoAsistencial.setEditable(true); 
        
        
                /**
         * Botón guardar consulta externa
         * Una vez que haya rellenado los campos pulsaré este botón para guardarlo en la bbdd
         */
          jbGuardarCE.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

            //pasar datetime a string y date a string
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy/MM/dd");
                int ID_ProcesoAsistencial = Integer.parseInt(jtfIdProcesoAsistencial.getText());//int
                int ID_CE = Integer.parseInt(jtfCE.getText());//int
                Time horaEntrada=Time.valueOf(jtfHoraEntrada.getText());
                
                //dateTime
                Time horaSalida = Time.valueOf(jtfHoraSalida.getText());;//dateTime
                String Especialidad = jtfEspecialidad.getText();
                Date fecha = null ;
                String fecha2= jtfFecha.getText(); 
                try {
                    fecha=(Date)sdf.parse(fecha2);
                   // horaEntrada=(Time) sdf.parse(horaEntrada2);
                } catch (ParseException ex) {
                    Logger.getLogger(JPCrearConsultasExternas.class.getName()).log(Level.SEVERE, null, ex);
                }
            


                   int codigo=JOptionPane.showConfirmDialog(null, "¿Quieres añadir una Consulta externa?", "Consulta Externa", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
                   if (codigo==JOptionPane.YES_OPTION){
                     System.out.println("Has pulsado en SI");
                  }else if(codigo==JOptionPane.NO_OPTION){
                     System.out.println("Has pulsado en NO");
                  }

               ConsultasExterna ce; 
               ce = new ConsultasExterna(fecha,  horaEntrada,  horaSalida,Especialidad,  ID_ProcesoAsistencial);
                
                BaseDatosConsultasExternas dbm = null;
                try {
                    dbm = new BaseDatosConsultasExternas();
                    dbm.addCE(ce);
                    System.out.println("CONSULTA EXTERNA CREADA");

                } catch (ClassNotFoundException ex) {
                    System.err.println("DRIVER NO ENCONTRADO.");
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    System.err.println("SQLEXCEPTION.");
                    ex.printStackTrace();
                } finally {
                    if (dbm != null) {
                        try {
                            dbm.closeConnection();
                        } catch (SQLException exc) {
                            System.err.println("SQLEXCEPTION DURANTE EL CIERRE.");
                            exc.printStackTrace();
                        }
                    }
                }
            }
        });
        
        
        
    }///////////////FIN

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jbGuardarCE = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfFecha = new javax.swing.JTextField();
        jtfEspecialidad = new javax.swing.JTextField();
        jtfIdProcesoAsistencial = new javax.swing.JTextField();
        jtfHoraEntrada = new javax.swing.JTextField();
        jtfHoraSalida = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jtfCE = new javax.swing.JTextField();

        setPreferredSize(new java.awt.Dimension(800, 600));

        jToolBar1.setRollover(true);

        jbGuardarCE.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/guardar.png"))); // NOI18N
        jbGuardarCE.setFocusable(false);
        jbGuardarCE.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbGuardarCE.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbGuardarCE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGuardarCEActionPerformed(evt);
            }
        });
        jToolBar1.add(jbGuardarCE);

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
        jLabel1.setText("CREAR UNA CONSULTA EXTERNA");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel2.setText("Fecha: ");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel3.setText("Hora de entrada: ");

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel4.setText("Hora de salida:");

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel5.setText("Especialidad:");

        jLabel6.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel6.setText("ID Proceso Asistencial: ");

        jLabel7.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel7.setText("ID Consultas Externas:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(50, 50, 50)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(jtfHoraEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jtfHoraSalida)
                                    .addComponent(jtfIdProcesoAsistencial))))
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtfEspecialidad, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfCE, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(55, 55, 55)
                                .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(233, 233, 233)
                        .addComponent(jLabel1)))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfIdProcesoAsistencial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jtfEspecialidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(80, 80, 80)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jtfHoraEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(78, 78, 78)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jtfHoraSalida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jtfCE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(192, 192, 192)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))))
                .addGap(0, 187, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbGuardarCEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGuardarCEActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbGuardarCEActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbGuardarCE;
    private javax.swing.JTextField jtfCE;
    private javax.swing.JTextField jtfEspecialidad;
    private javax.swing.JTextField jtfFecha;
    private javax.swing.JTextField jtfHoraEntrada;
    private javax.swing.JTextField jtfHoraSalida;
    private javax.swing.JTextField jtfIdProcesoAsistencial;
    // End of variables declaration//GEN-END:variables
}
